package top.appx.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import top.appx.dao.EmailDao;
import top.appx.entity.EmailEntity;
import top.appx.qo.EmailQO;
import top.appx.util.StringUtil;
import top.appx.vo.Pager;

import java.util.List;

/**
 * Created by zwyhome on 2015/6/17.
 */
@Repository
public class EmailDaoImpl extends BaseDaoImpl<EmailEntity> implements EmailDao {

    public Pager<EmailEntity> queryByPage(EmailQO bean, int page, int rows) {
        Criteria c = this.getCurrentSession().createCriteria(EmailEntity.class);
        int total = ((Number)c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
        c.setProjection(null);
        c.addOrder(Order.asc("id"));
        c.setFirstResult((page-1)*rows);
        c.setMaxResults(rows);
        if(bean!=null) {
            if(!StringUtil.isEmptyOrNull(bean.getTitle())) {
                c.add(Restrictions.like("title", "%" + bean.getTitle() + "%"));
            }
        }
        List list = c.list();
        Pager<EmailEntity> result = new Pager<EmailEntity>();
        result.setTotal(total);
        result.setRows(list);
        return result;
    }
}
